package com.cmwa.solution.sys.widget.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmwa.solution.base.controller.BaseController;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.business.infodisclosure.institution.entity.InstitutionVo;
import com.cmwa.solution.business.infodisclosure.institution.service.InstitutionService;
import com.cmwa.solution.business.product.entity.CompanyVo;
import com.cmwa.solution.business.product.entity.PrdAccountVo;
import com.cmwa.solution.business.product.entity.ProjectInfoVo;
import com.cmwa.solution.business.product.entity.SubPrdLineVo;
import com.cmwa.solution.business.product.service.CompanyService;
import com.cmwa.solution.business.product.service.SubPrdLineService;
import com.cmwa.solution.business.productManager.service.ProductManagerService;
import com.cmwa.solution.business.register.entity.CustInfoVo;
import com.cmwa.solution.business.register.service.SalesCapitalManageService;
import com.cmwa.solution.business.register.service.SalesDataCheckService;
import com.cmwa.solution.sys.dictionary.entity.DictionaryVo;
import com.cmwa.solution.sys.dictionary.service.DictionaryService;
import com.cmwa.solution.sys.menu.entity.MenuVo;
import com.cmwa.solution.sys.menu.service.MenuService;
import com.cmwa.solution.sys.organization.entity.DeptmentRelationshipVo;
import com.cmwa.solution.sys.organization.entity.OrganizationVo;
import com.cmwa.solution.sys.organization.service.OrganizationService;
import com.cmwa.solution.sys.permission.operRole.entity.OperRoleVo;
import com.cmwa.solution.sys.permission.operRole.service.OperRoleService;
import com.cmwa.solution.sys.permission.post.entity.PostVo;
import com.cmwa.solution.sys.permission.post.service.PostService;
import com.cmwa.solution.sys.userInfo.entity.UserInfoVo;
import com.cmwa.solution.sys.userInfo.service.UserInfoService;
import com.cmwa.solution.utils.SessionUtils;
import com.cmwa.solution.utils.cached.DepartmentCached;
import com.cmwa.solution.utils.cached.EmployeeCached;
/**
 *系统公共的控件、组件 调用的控制器
 *
 */
@Controller
@RequestMapping(value="/system/widget")
public class WidgetController extends BaseController {
	@Resource
	private DictionaryService dictionaryService;
	@Resource
	private MenuService menuService;
	@Resource
	private UserInfoService userInfoService;
	@Resource
	private OperRoleService operRoleService;
	@Resource 
	private PostService postService;
	@Resource
	private SubPrdLineService prdInfoService;
	@Resource
	private SalesDataCheckService salesDataCheckService;
	@Resource
	private CompanyService companyService;
	@Resource
	private InstitutionService institutionService;
	@Resource
	private ProductManagerService productManagerService;
	@Resource
	private SalesCapitalManageService salesCapitalManageService;
	@Resource
	private OrganizationService organizationService;
	
	@Resource
	private SubPrdLineService subPrdLineService;
	
	@RequestMapping("queryMatchMenu")
	@ResponseBody
	public List<MenuVo> queryMatchMenu(@RequestParam(value="menuNM",defaultValue = "") String menuNM) {
		List<MenuVo> list = menuService.queryMatchMenuList(menuNM);
		return list;
	}
	/**
	 * 查询用户信息
	 * @param response
	 * @return
	 */
	@RequestMapping("/queryMatchUserInfoList")
	@ResponseBody
	public List<UserInfoVo> queryMatchUserInfoList(@RequestParam(value = "assistantNM") String assistantNM, @RequestParam(value = "duplicate", defaultValue = "false") String duplicate,
			@RequestParam(value = "queryDimission", defaultValue = "false")boolean queryDimission,
			@RequestParam(value = "cleanDele", defaultValue = "false")boolean cleanDele, HttpServletResponse response) {
 		List<UserInfoVo> list = new ArrayList<UserInfoVo>();
 		if (StringUtils.isNotBlank(assistantNM)) {
			if (Boolean.parseBoolean(duplicate)) {
				list = EmployeeCached.getDuplicateUserInfosByName(assistantNM, queryDimission, cleanDele);
			} else {
				list = EmployeeCached.getUserInfosByName(assistantNM, queryDimission, cleanDele);
			}
		}
		return list;
	}

	/**
	 * 查询部门信息(未删除的)
	 * 
	 * @param response
	 * @return
	 */
	@RequestMapping("/queryMatchDepartmentList")
	@ResponseBody
	public List<DeptmentRelationshipVo> queryMatchDepartmentList(@RequestParam(value = "orgName") String orgName,
			@RequestParam(value = "isValid", defaultValue = "true")boolean isValid,
			@RequestParam(value = "leaveDept", defaultValue = "false")boolean leaveDept,HttpServletResponse response) {
		List<DeptmentRelationshipVo> list = new ArrayList<DeptmentRelationshipVo>();
		if (StringUtils.isNotBlank(orgName)) {
			list = DepartmentCached.getDepartmentsByName(orgName,isValid,leaveDept);
		}
		return list;
	}
	
	
	/**
	 * 查询全部部门(包含删除的，未删除的)
	 * 
	 * @param response
	 * @return
	 */
	@RequestMapping("/queryAllMatchDepartmentList")
	@ResponseBody
	public List<UserInfoVo> queryAllMatchDepartmentList(@RequestParam(value = "orgName") String orgName, HttpServletResponse response) {
		List<UserInfoVo> list = new ArrayList<UserInfoVo>();
		if (StringUtils.isNotBlank(orgName)) {
			list = userInfoService.queryAllMatchDepartmentList(orgName);
		}
		return list;
	}
	
	/**
	 * 只查询一级部门
	 */
	@RequestMapping("/queryMatchFirstDeptList")
	@ResponseBody
	public List<DeptmentRelationshipVo> queryMatchFirstDeptList(@RequestParam(value = "orgName") String orgName,
			@RequestParam(value = "isValid", defaultValue = "true")boolean isValid,
			@RequestParam(value = "leaveDept", defaultValue = "false")boolean leaveDept,HttpServletResponse response) {
		List<DeptmentRelationshipVo> list = new ArrayList<DeptmentRelationshipVo>();
		if (StringUtils.isNotBlank(orgName)) {
//			list = DepartmentUtil.getInstance().getFirstDeptsByName(orgName);
			list = DepartmentCached.getFirstDeptsByName(orgName,isValid,leaveDept);
		}
		return list;
	}

	@RequestMapping("queryMatchDictList")
	@ResponseBody
	public List<DictionaryVo> queryMatchDictList(DictionaryVo dictionaryVo,SearchParam sp){
		 List<DictionaryVo> dicts=dictionaryService.listDictionary(dictionaryVo);
		 List<DictionaryVo> list=new ArrayList<DictionaryVo>();
		 if (null!=dicts && dicts.size()>0) {//过滤掉根类型和值是一样的数据字典
			for (DictionaryVo dict:dicts) {
				if (!dict.getDctRootType().equals(dict.getDctValue()) && !StringUtils.equals("D", dict.getStat())) {
					list.add(dict);
				}
			}
		}
		return list;
	}
	
	/**
	 * 岗位下拉框
	 * @param postTitle
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryMatchPost")
	public List<PostVo> queryMatchPost(@RequestParam(value="postTitle",defaultValue = "") String postTitle) {
		List<PostVo> list = postService.queryMatchPost(postTitle);
		return list;
	}
	
	/**
	 * 操作角色下拉框
	 * @param roleName
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryMatchOperRole")
	public List<OperRoleVo> queryMatchOperRole(@RequestParam(value="roleName",defaultValue = "") String roleName) {
		List<OperRoleVo> list = operRoleService.queryAllRole(roleName);
		return list;
	}
	
	/**
	 * 系列产品下拉框
	 * @param postTitle
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryPrdInfoList")
	public List<ProjectInfoVo> queryPrdInfoList(@RequestParam(value="prjSname",defaultValue = "") String prdName, @RequestParam(value="manageCompany",defaultValue = "") String manageCompany) {
		SearchParam param = new SearchParam();
		String empId = null; 
		if (StringUtils.equals(SessionUtils.getEmployee().getEmpType(), "OUT")) {
		    // 传用户ID或管理人ID
			empId = SessionUtils.getEmployee().getID();
		}
		param.getSp().put("prjSname", prdName);
		param.getSp().put("manageCompany", manageCompany);
		param.getSp().put("empId", empId);
		return productManagerService.queryMatchProjectInfoList(param);
	}
	
	/**
	 * 产品下拉框
	 * @param postTitle
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryProductInfoList")
	public List<ProjectInfoVo> queryProductInfoList(@RequestParam(value="fundsname",defaultValue = "") String fundsname,@RequestParam(value="prjcode",defaultValue = "") String prjcode) {
		SearchParam param = new SearchParam();
		String empId = null; 
		if (StringUtils.equals(SessionUtils.getEmployee().getEmpType(), "OUT")) {
		    // 传用户ID或管理人ID
			empId = SessionUtils.getEmployee().getID();
		}
		param.getSp().put("fundsname", fundsname);
		param.getSp().put("prjcode", prjcode);
		param.getSp().put("empId", empId);
		return productManagerService.queryMatchProductInfoList(param);
	}
	
	/**
	 * 子产品下拉框
	 * @param postTitle
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryMatchSubPrdInfoList")
	public List<ProjectInfoVo> queryMatchSubPrdInfoList(@RequestParam(value="subFundSname",defaultValue = "") String prdName,@RequestParam(value="prdcode",defaultValue = "") String prdcode) {
		SearchParam param = new SearchParam();
		param.getSp().put("subFundSname", prdName);
		param.getSp().put("prdcode", prdcode);
		if (StringUtils.equals(SessionUtils.getEmployee().getEmpType(), "OUT")) {
			param.getSp().put("empId", SessionUtils.getEmployee().getID());
		}
		return productManagerService.queryMatchSubPrdInfoList(param);
	}
	
	/**
	 *  产品线子产品下拉框
	 * @param postTitle
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryMatchSubPrdLineList")
	public List<ProjectInfoVo> queryMatchSubPrdLineList(@RequestParam(value="subFundSname",defaultValue = "") String subFundSname) {
		SearchParam param = new SearchParam();
		param.getSp().put("subFundSname", subFundSname);
		return productManagerService.queryMatchSubPrdLineList(param);
	}
	
	
	/**
	 * 	客户信息下拉框
	 * @param custName
	 * @return
	 */
	@RequestMapping("queryCustInfoList")
	@ResponseBody
	public List<CustInfoVo> queryCustInfoList(@RequestParam(value="custname",defaultValue="") String custname){
		List<CustInfoVo> list = salesDataCheckService.queryCustInfoList(custname);
		return list;
	}
	
	/**
	 * 	查询字典类型
	 * @param dicType
	 * @return
	 */
	@RequestMapping("queryDictionaryList")
	@ResponseBody
	public List<DictionaryVo> queryDicInfoList(@RequestParam(value="dctType",defaultValue="") String dctType){
		DictionaryVo dicVo = new DictionaryVo();
		dicVo.setDctFathType(dctType);
		dicVo.setDctLeftType(dctType);
		dicVo.setDctRootType(dctType);
		List<DictionaryVo> list = dictionaryService.listDictionary(dicVo);
		return list;
	}
	
	/**
	 * 公司信息下拉框
	 * @param custName
	 * @return
	 */
	@RequestMapping("queryMatchCompany")
	@ResponseBody
	public List<CompanyVo> queryMatchCompany(CompanyVo companyVo){
		List<CompanyVo> list = companyService.queryMatchCompany(companyVo);
		return list;
	}
	
	/**
	 * 账户类型信息下拉框
	 * @param custName
	 * @return
	 */
	@RequestMapping("queryAccountTypeList")
	@ResponseBody
	public List<PrdAccountVo> queryAccountTypeList(@RequestParam(value="accCategory",defaultValue="") String accCategory,@RequestParam(value="accTypeName",defaultValue="") String accTypeName){
		SearchParam param = new SearchParam();
		param.getSp().put("accCategory", accCategory);
		param.getSp().put("accTypeName", accTypeName);
		List<PrdAccountVo> list = productManagerService.queryAccountTypeList(param);
		return list;
	}
	
	/**
	 * 账户信息信息下拉框
	 * @param custName
	 * @return
	 */
	@RequestMapping("queryBankAccountInfoList")
	@ResponseBody
	public List<PrdAccountVo> queryBankAccountInfoList(@RequestParam(value="accLevel",defaultValue="") String accLevel,@RequestParam(value="accBankName",defaultValue="") String accBankName){
		SearchParam param = new SearchParam();
		param.getSp().put("accLevel", accLevel);
 		param.getSp().put("accBankName", accBankName);
		List<PrdAccountVo> list = productManagerService.queryBankAccountInfoList(param);
		return list;
	}
	
	/**
	 * 机构信息下拉框
	 * @param custName
	 * @return
	 */
	@RequestMapping("queryMatchInst")
	@ResponseBody
	public List<InstitutionVo> queryMatchInst(InstitutionVo institutionVo){
		List<InstitutionVo> list = institutionService.getInstitutionList(institutionVo);
		return list;
	}
	
	/**
	 * 	客户权益产品下拉框
	 * @param prdName
	 * @return
	 */
	@ResponseBody
	@RequestMapping("custRegisQueryPrdInfoList")
	public List<ProjectInfoVo> custRegisProjectInfo(@RequestParam(value="prdName",defaultValue = "") String prdName) {
		SearchParam sp = new SearchParam();
		sp.getSp().put("prdsname", prdName);
		return salesCapitalManageService.custRegisProjectInfo(sp);
	}
	
	/**
	 * 招商财富产品线产品下拉框
	 * @param subFundSname
	 * @return
	 */
	@ResponseBody
	@RequestMapping("prdLineInfoList")
	public List<SubPrdLineVo> prdLineInfo(@RequestParam(value="subFundSname",defaultValue = "") String subFundSname) {
		SearchParam sp = new SearchParam();
		sp.getSp().put("subFundSname", subFundSname);
		return subPrdLineService.prdLineInfo(sp);
	}
	
	@ResponseBody
	@RequestMapping("queryOrgChilds")
	public List<OrganizationVo> queryOrgChilds(@RequestParam("reOrgId") String reOrgId, @RequestParam("orgName") String orgName, @RequestParam(value = "limit", required=false) String limit) {
		List<OrganizationVo> list = new ArrayList<OrganizationVo>();
		SearchParam sp = new SearchParam();
		sp.getSp().put("limit", limit);//限制查出条数
		sp.getSp().put("reOrgId", reOrgId);//限制查出条数
		sp.getSp().put("orgName", orgName);//限制查出条数
		list = organizationService.queryOrgChilds(sp);
		return list;
	}
	@ResponseBody
	@RequestMapping("queryTitleChilds")
	public List<OrganizationVo> queryTitleChilds(@RequestParam("orgId") String orgId, @RequestParam("titleName") String titleName, @RequestParam(value = "limit", required=false) String limit) {
		List<OrganizationVo> list = new ArrayList<OrganizationVo>();
		SearchParam sp = new SearchParam();
		sp.getSp().put("limit", limit);//限制查出条数
		sp.getSp().put("orgId", orgId);//限制查出条数
		sp.getSp().put("titleName", titleName);//限制查出条数
		list = organizationService.queryTitleChilds(sp);
		return list;
	}
}
